

#include "stdafx.h"
#include "pf_actioninterface.h"

/**
 * Constructor.
 *
 * Sets the entity container on which the action class inherited 
 * from this interface operates.
 *
 * @param name Action name. This can be used internally for
 *             debugging mainly.
 * @param container Entity container this action operates on.
 * @param graphicView Graphic view instance this action operates on.
 *                    Please note that an action belongs to this
 *                    view.
 * @param cursor Default mouse cursor for this action. If the action
 *               is suspended and resumed again the cursor will always
 *               be reset to the one given here.
 */
PF_ActionInterface::PF_ActionInterface(const char* name,
                                       PF_Container& container,
                                       PF_GraphicView& graphicView) :
PF_Snapper(container, graphicView) {

    PF_DEBUG->print("PF_ActionInterface::PF_ActionInterface: Setting up action: \"%s\"", name);
	
    this->name = name;
    status = 0;
    finished = false;
    //triggerOnResume = false;

    // graphic provides a pointer to the graphic if the
    // entity container is a graphic (i.e. can also hold
    // layers).
    graphic = container.getGraphic();

    // document pointer will be used for undo / redo
    document = container.getDocument();

    //this->cursor = cursor;
    //setSnapMode(graphicView.getDefaultSnapMode());


    PF_DEBUG->print("PF_ActionInterface::PF_ActionInterface: Setting up action: \"%s\": OK", name);
}

/**
 * Destructor.
 */
PF_ActionInterface::~PF_ActionInterface() {
    // would be pure virtual now:
    // hideOptions();
}

/**
 * Must be implemented to return the ID of this action.
*
* @todo no default implementation
 */
LX_Define::ActionType PF_ActionInterface::rtti()  {
    return LX_Define::ActionNone;
}

/**
 * @return name of this action
 */
PF_String PF_ActionInterface::getName() {
    return name;
}

/**
 * Called to initiate an action. This funtcion is often
 * overwritten by the implementing action.
 *
 * @param status The status on which to initiate this action.
 * default is 0 to begin the action.
 */
void PF_ActionInterface::init(int status) {
    PF_Snapper::init();

    setStatus(status);
    if (status>=0) {
        //graphicView->setMouseCursor(cursor);
		updateMouseButtonHints();
        updateMouseCursor();
        updateToolBar();
    }
}



/**
 * Called when the mouse moves and this is the current action. 
 * This function can be overwritten by the implementing action.
 * The default implementation keeps track of the mouse position.
 */
void PF_ActionInterface::mouseMoveEvent(PF_MouseEvent*) {}

/**
 * Called when the left mouse button is pressed and this is the 
 * current action. 
 * This function can be overwritten by the implementing action.
 * The default implementation does nothing.
 */
void PF_ActionInterface::mousePressEvent(PF_MouseEvent*) {}

/**
 * Called when the left mouse button is released and this is 
 * the current action. 
 * This function can be overwritten by the implementing action.
 * The default implementation does nothing.
 */
void PF_ActionInterface::mouseReleaseEvent(PF_MouseEvent*) {}

/**
 * Called when a key is pressed and this is the current action.
 * This function can be overwritten by the implementing action.
 * The default implementation does nothing.
 */
void PF_ActionInterface::keyPressEvent(PF_KeyEvent* e) {
    e->ignore();
}

/**
 * Called when a key is released and this is the current action.
 * This function can be overwritten by the implementing action.
 * The default implementation does nothing.
 */
void PF_ActionInterface::keyReleaseEvent(PF_KeyEvent* e) {
    e->ignore();
}

/**
 * Coordinate event. Triggered usually from a command line.
 * This function can be overwritten by the implementing action.
 * The default implementation does nothing.
 */
void PF_ActionInterface::coordinateEvent(PF_CoordinateEvent*) {}

/**
 * Called when a command from the command line is launched.
 * and this is the current action.
 * This function can be overwritten by the implementing action.
 * The default implementation does nothing.
 */
void PF_ActionInterface::commandEvent(PF_CommandEvent*) {
}

/**
 * Must be implemented to return the currently available commands
 *  for the command line.
 */
PF_StringList PF_ActionInterface::getAvailableCommands() {
    PF_StringList l;
    return l;
}

/**
 * Sets the current status (progress) of this action.
 * The default implementation sets the class variable 'status' to the
 * given value and finishes the action if 'status' is negative.
 *
 * @param status Status number. It's up to the action implementor
 *               what the action uses the status for. However, a
 *               negative status number finishes the action. Usually
 *               the status of an action increases for every step
 *               of progress and decreases when the user goes one
 *               step back (i.e. presses the right mouse button).
 */
void PF_ActionInterface::setStatus(int status) {
    this->status = status;
    if (status<0) {
        finish();
        status=0;
    }
    updateMouseButtonHints();
    updateToolBar();
    updateMouseCursor();
}

/**
 * @return Current status of this action.
 */
int PF_ActionInterface::getStatus() {
    return status;
}

/**
 * Triggers this action. This should be called after all
 * data needed for this action was collected / set.
 * The default implementation does nothing.
 */
void PF_ActionInterface::trigger() {}

/**
 * Should be overwritten to update the mouse button hints
 * wherever they might needed.
 */
void PF_ActionInterface::updateMouseButtonHints() {}

/**
 * Should be overwritten to set the mouse cursor for this action.
 */
void PF_ActionInterface::updateMouseCursor() {}

/**
 * Should be overwritten to set the toolbar for this action.
 */
void PF_ActionInterface::updateToolBar() {}

/**
 * @return true, if the action is finished and can be deleted.
 */
bool PF_ActionInterface::isFinished() {
    return finished;
}


/**
 * Forces a termination of the action without any cleanup.
 */
void PF_ActionInterface::setFinished() {
	status = -1;
}


/**
 * Finishes this action.
 */
void PF_ActionInterface::finish() {
	PF_DEBUG->print("PF_ActionInterface::finish");
    status = -1;
    finished = true;
    graphicView->setMouseCursor(LX_Define::ArrowCursor);
    updateToolBar();
    hideOptions();
    PF_Snapper::finish();
	PF_DEBUG->print("PF_ActionInterface::finish: OK");
}

/**
 * Called by the event handler to give this action a chance to
 * communicate with its predecessor.
 */
void PF_ActionInterface::setPredecessor(PF_ActionInterface* pre) {
    predecessor = pre;
}

/**
 * Suspends this action while another action takes place.
 */
void PF_ActionInterface::suspend() {
    graphicView->setMouseCursor(LX_Define::ArrowCursor);
    PF_Snapper::suspend();
}

/**
 * Resumes an action after it was suspended.
 */
void PF_ActionInterface::resume() {
    updateMouseCursor();
    updateToolBar();
    PF_Snapper::resume();
}

/**
 * Hides the tool options. Default implementation does nothing.
 */
void PF_ActionInterface::hideOptions() {
    PF_Snapper::hideOptions();
}

/**
 * Shows the tool options. Default implementation does nothing.
 */
void PF_ActionInterface::showOptions() {
    PF_Snapper::showOptions();
}

/**
 * Calls checkCommand() from the PF_COMMANDS module.
 */
bool PF_ActionInterface::checkCommand(const PF_String& cmd, const PF_String& str,
                                      LX_Define::ActionType action) {
    return PF_COMMANDS->checkCommand(cmd, str, action);
}

/**
 * Calls command() from the PF_COMMANDS module.
 */
PF_String PF_ActionInterface::command(const PF_String& cmd) {
	return PF_COMMANDS->command(cmd);
}

/**
 * Calls msgAvailableCommands() from the PF_COMMANDS module.
 */
PF_String PF_ActionInterface::msgAvailableCommands() {
	return PF_COMMANDS->msgAvailableCommands();
}

